package com.mapaoer.user.service.impl;

import com.mapaoer.user.dao.EnterpriseProfileRepository;
import com.mapaoer.user.dto.EnterpriseProfileDTO;
import com.mapaoer.user.entity.EnterpriseProfile;
import com.mapaoer.user.service.EnterpriseProfileService;
import com.mapaoer.user.entity.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.time.LocalDateTime;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 企业资料服务实现类
 * 实现EnterpriseProfileService接口定义的企业资料相关业务操作
 */
@Service
public class EnterpriseProfileServiceImpl implements EnterpriseProfileService {

    @Autowired
    private EnterpriseProfileRepository enterpriseProfileRepository;

    @Override
    @Transactional
    public boolean createEnterpriseProfile(EnterpriseProfileDTO enterpriseProfileDTO) {
        // 检查是否已存在该用户的企业资料
        User user = new User();
        user.setId(enterpriseProfileDTO.getUserId());
        if (enterpriseProfileRepository.findByUser(user).isPresent()) {
            return false;
        }

        EnterpriseProfile enterpriseProfile = new EnterpriseProfile();
        BeanUtils.copyProperties(enterpriseProfileDTO, enterpriseProfile);
        enterpriseProfile.setCreateTime(LocalDateTime.now());
        enterpriseProfile.setUpdateTime(LocalDateTime.now());

        enterpriseProfileRepository.insert(enterpriseProfile);
        return true;
    }

    @Override
      @Override
      @Transactional
    public boolean updateEnterpriseProfile(Long id, EnterpriseProfileDTO enterpriseProfileDTO) {
        // 检查企业资料是否存在
        // 查询并验证企业资料
        final EnterpriseProfile enterpriseProfile = enterpriseProfileRepository.selectById(id);
        if (enterpriseProfile == null) {
            return false;
        }


        BeanUtils.copyProperties(enterpriseProfileDTO, enterpriseProfile);
        enterpriseProfile.setUpdateTime(LocalDateTime.now());
        enterpriseProfileRepository.updateById(enterpriseProfile);
        return true;
    }

    @Override
    @Transactional
    public boolean deleteEnterpriseProfile(Long id) {
        EnterpriseProfile enterpriseProfile = enterpriseProfileRepository.selectById(id);
        if (enterpriseProfile == null) {
            return false;
        }
        enterpriseProfileRepository.deleteById(id);
        return true;
    }

    @Override
    public EnterpriseProfileDTO getEnterpriseProfileById(Long id) {
        EnterpriseProfile enterpriseProfile = enterpriseProfileRepository.selectById(id);
        if (enterpriseProfile == null) {
            return null;
        }

        return convertToDTO(enterpriseProfile);
    }

    @Override
    public EnterpriseProfileDTO getEnterpriseProfileByUserId(Long userId) {
        User user = new User();
        user.setId(userId);
        EnterpriseProfile enterpriseProfile = enterpriseProfileRepository.selectOne(new QueryWrapper<EnterpriseProfile>().eq("user_id", user.getId()));
        if (enterpriseProfile == null) {
            return null;
        }
        return convertToDTO(enterpriseProfile);
    }

    @Override
    public List<EnterpriseProfileDTO> searchEnterprisesByName(String companyName) {
        List<EnterpriseProfile> profiles = enterpriseProfileRepository.findByCompanyNameContaining(companyName);
        return profiles.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    @Override
    public List<EnterpriseProfileDTO> searchEnterprisesByIndustry(String industry) {
        List<EnterpriseProfile> profiles = enterpriseProfileRepository.findByIndustry(industry);
        return profiles.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    /**
     * 将EnterpriseProfile实体转换为EnterpriseProfileDTO
     */
    private EnterpriseProfileDTO convertToDTO(EnterpriseProfile enterpriseProfile) {
        EnterpriseProfileDTO dto = new EnterpriseProfileDTO();
        BeanUtils.copyProperties(enterpriseProfile, dto);
        return dto;
    }
}